package com.aos.eap.activity;

import android.os.Handler;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aos.eap.R;
import com.aos.eap.base.BaseDialogActivity;
import com.aos.eap.base.Constant;
import com.aos.eap.bean.WireInfoBean;
import com.aos.eap.bean.net.BaseResponseBean;
import com.aos.eap.utils.MyUtils;
import com.aos.eap.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;

public class MFG_WireUpEqpActivity extends BaseDialogActivity {


    @BindView(R.id.tvUser)
    TextView tvUser;
    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.etNewWireId)
    FreeUI_ClearEditText etNewWireId;
    @BindView(R.id.tvNewWireThingId)
    TextView tvNewWireThingId;
    @BindView(R.id.tvNewWireLotId)
    TextView tvNewWireLotId;
    @BindView(R.id.tvThingTime)
    TextView tvThingTime;
    @BindView(R.id.tvWeight)
    TextView tvWeight;
    @BindView(R.id.tvLong)
    TextView tvLong;
    @BindView(R.id.tvLostTime)
    TextView tvLostTime;
    @BindView(R.id.tvDelayTime)
    TextView tvDelayTime;
    @BindView(R.id.etRemark)
    FreeUI_ClearEditText etRemark;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private WireInfoBean wireInfoBean;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_wire_up_eqp;
    }

    @Override
    protected void initView() {
        tvUser.setText(Constant.CURRENT_USER);
        MyUtils.setEditTextListener(etEqpId, this, s -> MyUtils.setEditTextFocus(etLotId));
        MyUtils.setEditTextListener(etLotId, this, s -> MyUtils.setEditTextFocus(etNewWireId));
        MyUtils.setEditTextListener(etNewWireId, this, s -> requestWireInfo());
    }

    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("Wire上机");
        freeUI_titleView.setLeftListener(view -> showBackDialog(MFG_WireUpEqpActivity.this));
        freeUI_titleView.setRightListener("提交", v -> {
            if (MyUtils.isFastClick()){
                showToast("您点击的太快了",false);
                return;
            }
            if (wireInfoBean == null) {
                showToast("请先扫码获取wire信息", false);
                etEqpId.setText("");
                MyUtils.setEditTextFocus(etEqpId);
                return;
            }
            String newWireId = etNewWireId.getText().toString().trim();
            if (FreeApi_StringUtils.isEmpty(newWireId)) {
                showToast("请扫描新线卷轴编号", false);
                MyUtils.setEditTextFocus(etNewWireId);
                return;
            }
            JsonObject map = new JsonObject();
            map.addProperty("EqpID", wireInfoBean.getEqpID());
            map.addProperty("LotID", wireInfoBean.getLotID());
            map.addProperty("WireMatID", wireInfoBean.getWireMatID());
            map.addProperty("WireLotID", newWireId);
            map.addProperty("WireBatchNo", wireInfoBean.getWireBatchNo());
            map.addProperty("WireSendTime", wireInfoBean.getWireSendTime());
            map.addProperty("WireWeight", wireInfoBean.getWireWeight());
            map.addProperty("WireLength", wireInfoBean.getWireLength());
            map.addProperty("EqpWireID", wireInfoBean.getEqpWireID());
            map.addProperty("EqpWireRemainTime", wireInfoBean.getEqpWireRemainTime());
            map.addProperty("EqpWireExpireTime", wireInfoBean.getEqpWireExpireTime());
            map.addProperty("Remark", etRemark.getText().toString().trim());
            requestSubmit(map);
        });
        return new FreeUI_AddViewEntity("header", llMain);
    }

    private void requestSubmit(JsonObject map) {
        NetUtils.requestGetNet(this, "EMS_AddWire", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<?>>(
                new TypeToken<BaseResponseBean<?>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<?>> response) {
                if (response.body().isSuccess(getApplicationContext(), false,getSupportFragmentManager())) {
                    showToast("提交成功", true);
                    new Handler().postDelayed(MFG_WireUpEqpActivity.this::finish, 2000);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<?>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_AddWire");
            }
        });
    }

    private void requestWireInfo() {
        String eqpId = etEqpId.getText().toString().trim();
        if (FreeApi_StringUtils.isEmpty(eqpId)) {
            showToast("请扫描设备编号", false);
            etEqpId.setText("");
            MyUtils.setEditTextFocus(etEqpId);
            return;
        }
        String lotId = etLotId.getText().toString().trim();
        if (FreeApi_StringUtils.isEmpty(lotId)) {
            showToast("请扫描批次编号", false);
            MyUtils.setEditTextFocus(etLotId);
            return;
        }
        String wireId = etNewWireId.getText().toString().trim();
        if (FreeApi_StringUtils.isEmpty(wireId)) {
            showToast("请扫描卷轴编号", false);
            MyUtils.setEditTextFocus(etLotId);
            return;
        }
        JsonObject map = new JsonObject();
        map.addProperty("EqpID", eqpId);
        map.addProperty("LotID", lotId);
        map.addProperty("WireLotId", wireId);
        NetUtils.requestGetNet(this, "EMS_AddWireLoad", map, new FreeUI_DialogEntityCallBack<BaseResponseBean<WireInfoBean>>(
                new TypeToken<BaseResponseBean<WireInfoBean>>() {
                }.getType(), getSupportFragmentManager(), this) {
            @Override
            public void onSuccess(Response<BaseResponseBean<WireInfoBean>> response) {
                if (response.body().isSuccess(getApplicationContext(), true,getSupportFragmentManager())) {
                    wireInfoBean = response.body().getResponse().getBody();
                   setPageData(wireInfoBean);
                }else {
                    wireInfoBean = null;
                    setPageData(new WireInfoBean());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<WireInfoBean>> response) {
                super.onError(response);
                uploadLog(response.getException());
                loadError(response.getException(), "EMS_AddWireLoad");
                wireInfoBean = null;
                setPageData(new WireInfoBean());
            }
        });
    }

    private void setPageData(WireInfoBean wireInfoBean) {
        tvNewWireThingId.setText(wireInfoBean.getWireMatID());
        tvNewWireLotId.setText(wireInfoBean.getWireBatchNo());
        tvThingTime.setText(wireInfoBean.getWireSendTime());
        tvWeight.setText(String.valueOf(wireInfoBean.getWireWeight()));
        tvLong.setText(String.valueOf(wireInfoBean.getWireLength()));
        tvLostTime.setText(wireInfoBean.getEqpWireRemainTime());
        tvDelayTime.setText(wireInfoBean.getEqpWireExpireTime());
    }
}
